package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2021/7/19.<br><br>
 *
 * 完美数
 */
public class Topic507 {
    public static void main(String[] args) {
        Topic507 instance = new Topic507();

        System.out.println(instance.checkPerfectNumber(28));
        System.out.println(instance.checkPerfectNumber(6));
        System.out.println(instance.checkPerfectNumber(496));
        System.out.println(instance.checkPerfectNumber(8128));
        System.out.println(instance.checkPerfectNumber(2));
    }

    public boolean checkPerfectNumber(int num) {
        if(num < 3) {
            return false;
        }

        int sum = 1;
        for(int i = 2; i * i <= num; i++) {
            if(num % i == 0) {
                sum += i + num / i;
            }
        }
        return sum == num;
    }

    public boolean checkPerfectNumber2(int num) {
        int sum = 0;
        for(int i = num / 2; i >= 1; i--) {
            if(num % i == 0) {
                sum += i;
            }
        }

        return sum == num;
    }
}
